package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.imageload.ImageLoad;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2015/12/29.
 */
public class SelectShopAdapter extends BaseAdapter{
    private final Context mContext;

    private List<JSONObject> data;

    public SelectShopAdapter(Context context) {
        mContext = context;
    }

    class ViewHolder {
        ImageView logoI;
        TextView introduceT;
        ImageView checkI;
    }

    public void setData(List<JSONObject> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (data == null) {
            return 0;
        }
        return data.size();
    }

    public JSONObject getItem(int position) {
        if (data == null) {
            return null;
        }
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_select_shop, parent, false);
            holder.logoI = (ImageView) convertView.findViewById(R.id.logo);
            holder.introduceT = (TextView) convertView.findViewById(R.id.introduce);
            holder.checkI = (ImageView) convertView.findViewById(R.id.check);

            convertView.setTag(holder);
        }else {
            holder =  (ViewHolder)convertView.getTag();
        }

        final JSONObject jo = getItem(position);
        try {
            ImageLoad.LoadImage(holder.logoI ,  jo.getString("logo") , R.drawable.trans ,R.drawable.trans,mContext);
            holder.introduceT.setText(jo.getString("description"));
            boolean ischeck = jo.getBoolean("check");
            if (ischeck){
                holder.checkI.setImageResource(R.drawable.checked);
            }else {
                holder.checkI.setImageResource(R.drawable.uncheck);
            }
        } catch (JSONException e) {
        }
        return convertView;
    }

}
